package com.ylr.blog.common.enums;

import com.ylr.base.common.dto.NameValue;
import lombok.Getter;
import lombok.ToString;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-19 10:39:09
 * className: BlogEnum
 * version: 1.0
 * description:
 */
@Getter
@ToString
public enum BlogEnum {

    /*
        博客表字段枚举值
     */
    // 状态
    SAVE(0, 0, "草稿"),
    AUDIT(0, 1, "提交审核"),
    AUDIT_WITHDRAW(0, 2, "审核撤回"),
    AUDIT_PASS(0, 3, "审核通过"),
    AUDIT_REJECT(0, 4, "审核拒绝"),
    RECHECK(0, 5, "复审"),
    RECHECK_PASS(0, 6, "复审通过"),
    RECHECK_REJECT(0, 7, "复审拒绝"),
    PUBLISH(1, 100, "发布"),
    EDIT_BLOG(1, 101, "编辑已发布博客"),
    HOT_BLOG(1, 102, "热门博客"),
    CANCEL_HOT(1, 103, "取消热门"),
    TOP_BLOG(1, 104, "置顶博客"),
    CANCEL_TOP(1, 105, "取消置顶"),
    HIDE(1, 106, "隐藏"),
    SHOW(1, 107, "展示"),
    REPUBLISH(1, 108, "重新发布"),
    OUT(2, 200, "下架"),
    APPEAL(2, 201, "博客申诉"),
    APPEAL_PASS(2, 202, "博客申诉通过"),
    APPEAL_REJECT(2, 203, "博客申诉拒绝"),

    // 类型[0-原创 1-转载 2-翻译]
    AUTHOR_TYPE(0, "原创"), REPRINT_TYPE(1, "转载"), TRANSLATE_TYPE(2, "翻译"),
    // 置顶状态[0-正常 1-置顶]
    TOP_NO(0, "正常"), TOP_YES(1, "置顶"),
    // 热门状态[0-正常 1-热门]
    HOT_NO(0, "正常"), HOT_YES(1, "热门"),
    // 版权声明状态[0-显示 1-隐藏]
    COPYRIGHT_SHOW(0, "显示"), COPYRIGHT_HIDE(1, "隐藏"),
    // 评论状态[0-开启 1-关闭]
    COMMENT_OPEN(0, "开启"), COMMENT_CLOSE(1, "关闭"),
    // 自动发布状态[0-开启 1-关闭]
    AUTO_PUBLISH_OPEN(0, "开启"), AUTO_PUBLISH_CLOSE(1, "关闭"),
    ;

    /**
     * 阶段[0-创建 1-发布 2-下架]
     */
    private final Integer stage;

    /**
     * 值
     */
    private final Integer value;

    /**
     * 名称
     */
    private final String name;

    BlogEnum(Integer value, String name) {
        this.stage = null;
        this.value = value;
        this.name = name;
    }

    BlogEnum(Integer stage, Integer value, String name) {
        this.stage = stage;
        this.value = value;
        this.name = name;
    }

    /**
     * 获取博客状态枚举数组
     */
    public static BlogEnum[] getStatusEnums() {
        return new BlogEnum[] {
                SAVE,
                AUDIT,
                AUDIT_WITHDRAW,
                AUDIT_PASS,
                AUDIT_REJECT,
                RECHECK,
                RECHECK_PASS,
                RECHECK_REJECT,
                PUBLISH,
                EDIT_BLOG,
                HOT_BLOG,
                CANCEL_HOT,
                TOP_BLOG,
                CANCEL_TOP,
                HIDE,
                SHOW,
                OUT,
                REPUBLISH,
                APPEAL,
                APPEAL_PASS,
                APPEAL_REJECT
        };
    }

    /**
     * 获取博客状态枚举列表
     * @return 博客状态枚举列表
     */
    public static List<NameValue<Integer>> getStatusEnumList() {
        return getEnumList(getStatusEnums());
    }

    /**
     * 获取博客状态枚举对象
     * @param value 参数值
     * @return 博客枚举对象
     */
    public static BlogEnum getStatusEnum(Integer value) {
        BlogEnum[] statusEnums = getStatusEnums();
        for (BlogEnum statusEnum : statusEnums) {
            if (statusEnum.getValue().equals(value)) {
                return statusEnum;
            }
        }
        return null;
    }

    /**
     * 校验状态枚举值
     * @param value 值
     * @return true-错误 false-正确
     */
    public static boolean isStatusFail(Integer value) {
        return Objects.isNull(getStatusEnum(value));
    }

    /**
     * 获取博客类型枚举数组
     */
    public static BlogEnum[] getTypeEnums() {
        return new BlogEnum[] {
                AUTHOR_TYPE, REPRINT_TYPE, TRANSLATE_TYPE
        };
    }

    /**
     * 获取博客类型枚举列表
     */
    public static List<NameValue<Integer>> getTypeEnumList() {
        return getEnumList(getTypeEnums());
    }

    /**
     * 获取博客类型枚举对象
     * @param value 参数值
     * @return 博客枚举对象
     */
    public static BlogEnum getTypeEnum(Integer value) {
        BlogEnum[] typeEnums = getTypeEnums();
        for (BlogEnum typeEnum : typeEnums) {
            if (typeEnum.getValue().equals(value)) {
                return typeEnum;
            }
        }
        return null;
    }

    /**
     * 博客枚举数组封装
     * @param blogEnums 博客枚举数组
     * @return 博客枚举列表
     */
    private static List<NameValue<Integer>> getEnumList(BlogEnum[] blogEnums) {
        return Stream.of(blogEnums)
                .map(blogEnum -> new NameValue<>(blogEnum.name, blogEnum.value))
                .collect(Collectors.toList());
    }

}
